<?php

namespace App\Admin\Controllers;

use Encore\Admin\Grid;
use App\Models\GoldBean;
use Encore\Admin\Controllers\AdminController;

class GoldBeansController extends AdminController
{
    protected $title = '金店金豆';

    protected function grid()
    {
        $grid = new Grid(new GoldBean());
	    $grid->model()->orderByDesc('id');

	    if ($userId = request('user_id')) {
		    $grid->model()->where('user_id', $userId)->orderByDesc('id');
	    }

        $grid->column('id', 'ID');
	    $grid->column('user', '会员')->display(function () {
		    return $this->user ? $this->user->nameText(true) : '';
	    });
        $grid->column('money', '金豆');
	    $grid->column('origin', '原额');
	    $grid->column('result', '现额');
	    $grid->column('reason', '事由');
	    $grid->column('data', '数据');
	    $grid->column('created_at', '时间')->display(function () {
		    return (string)$this->created_at;
	    });

	    $this->gridFilter($grid);

	    $grid->disableRowSelector();
	    $grid->disableCreateButton();
	    $grid->disableExport();
	    $grid->disableActions();

        return $grid;
    }

	protected function gridFilter(Grid $grid)
	{
		$grid->filter(function (Grid\Filter $filter) {
			$filter->expand();
			$filter->column(1/2, function (Grid\Filter $filter) {
				$filter->disableIdFilter();
				$filter->equal('user_id', '会员 ID 查询');
			});
		});
	}
}
